/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.myorg.quickstart;

import org.apache.flink.api.common.serialization.SerializationSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.pulsar.FlinkPulsarSink;
import org.apache.flink.streaming.connectors.pulsar.FlinkPulsarSource;
import org.apache.flink.streaming.util.serialization.PulsarPrimitiveSchema;
import org.apache.flink.streaming.util.serialization.PulsarSerializationSchemaWrapper;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.impl.conf.ClientConfigurationData;

import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.apache.flink.table.api.DataTypes;

import java.util.Optional;

import static org.apache.flink.streaming.connectors.pulsar.internal.PulsarOptions.*;



/**
 * Skeleton for a Flink Streaming Job.
 *
 * <p>For a tutorial how to write a Flink streaming application, check the
 * tutorials and examples on the <a href="https://flink.apache.org/docs/stable/">Flink Website</a>.
 *
 * <p>To package your application into a JAR file for execution, run
 * 'mvn clean package' on the command line.
 *
 * <p>If you change the name of the main class (with the public static void main(String[] args))
 * method, change the respective entry in the POM.xml file (simply search for 'mainClass').
 */
public class StreamingJob {

	public static void main(String[] args) throws Exception {
		// set up the streaming execution environment


		//StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(configuration);

		final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

		/*
		 * Here, you can start creating your execution plan for Flink.
		 *
		 * Start with getting some data from the environment, like
		 * 	env.readTextFile(textPath);
		 *
		 * then, transform the resulting DataStream<String> using operations
		 * like
		 * 	.filter()
		 * 	.flatMap()
		 * 	.join()
		 * 	.coGroup()
		 *
		 * and many more.
		 * Have a look at the programming guide for the Java API:
		 *
		 * https://flink.apache.org/docs/latest/apis/streaming/index.html
		 *
		 */

		// execute program

		//StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
		env.setParallelism(1);

		String serviceUrl = "pulsar://localhost:6650";
		String adminUrl = "http://localhost:8080";

		int numElements = 10;
		String topic = "source-topic";
	/*	List<Integer> expectList = IntStream.range(0, numElements)
				.mapToObj(value -> Integer.valueOf(value))
				.collect(Collectors.toList());
		DataStreamSource<Integer> rowDataStreamSource = env.fromCollection(expectList);
		ClientConfigurationData clientConfigurationData = new ClientConfigurationData();
		clientConfigurationData.setServiceUrl(serviceUrl);

		rowDataStreamSource.addSink(new FlinkPulsarSink(adminUrl, Optional.of(topic),
				clientConfigurationData, new Properties(),
				new PulsarSerializationSchemaWrapper.Builder<>
						((SerializationSchema<Integer>) element -> Schema.INT32.encode(element))
						.useAtomicMode(DataTypes.INT())
						.build()));
*/

	/*	List<String> expectList = IntStream.range(0, numElements)
				.mapToObj(value -> String.valueOf(value))
				.collect(Collectors.toList());
		DataStreamSource<String> rowDataStreamSource = env.fromCollection(expectList);
		ClientConfigurationData clientConfigurationData = new ClientConfigurationData();
		clientConfigurationData.setServiceUrl(serviceUrl);

		rowDataStreamSource.addSink(new FlinkPulsarSink(adminUrl, Optional.of(topic),
				clientConfigurationData, new Properties(),
				new PulsarSerializationSchemaWrapper.Builder<>
						((SerializationSchema<String>) element -> Schema.STRING.encode(element))
						.useAtomicMode(DataTypes.STRING())
						.build()));*/

		List<Integer> expectList = IntStream.range(0, numElements)
				.mapToObj(value -> Integer.valueOf(value))
				.collect(Collectors.toList());
		DataStreamSource<Integer> rowDataStreamSource = env.fromCollection(expectList);
		ClientConfigurationData clientConfigurationData = new ClientConfigurationData();
		clientConfigurationData.setServiceUrl(serviceUrl);

		rowDataStreamSource.addSink(new FlinkPulsarSink(adminUrl, Optional.of(topic),
				clientConfigurationData, new Properties(),
				new PulsarSerializationSchemaWrapper.Builder<>
						((SerializationSchema<Integer>) element -> Schema.INT32.encode(element))
						.useAtomicMode(DataTypes.INT())
						.build()));


		//TestUtils.tryExecute(see, "test sink data to pulsar");

		//StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
		//env.setParallelism(1);
		/*Properties properties = new Properties();
		properties.put(TOPIC_SINGLE_OPTION_KEY, topic);
		FlinkPulsarSource<Integer> integerFlinkPulsarSource = new FlinkPulsarSource<>(serviceUrl, adminUrl,
				new PulsarPrimitiveSchema<>(Integer.class), properties).setStartFromEarliest();
		      env
				.addSource(integerFlinkPulsarSource)
				.map(Object::toString)
				.map(new FailingIdentityMapper<>(expectList.size()))
				.addSink(new SingletonStreamSink.StringSink<>()).setParallelism(1);

		//TestUtils.tryExecute(env, "test read data from pulsar");

		SingletonStreamSink.compareWithList(expectList.subList(0, expectList.size() - 1).stream().map(Object::toString)
				.collect(Collectors.toList()));*/

		env.execute("Flink Streaming Java API Skeleton");
	}
}
